// subPages/pensionService/electronicFence/electronicFence.js
// 引入SDK核心类
const app = getApp();
var util = require('../../../utils/util');
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      // motto: '示例小程序-获取当前地理位、速度',
      // userInfo: {},
      hasLocation:false,
      location:{},
      notSafe:true
  },
   toSet(){
      wx.navigateTo({
          url:'/subPages/pensionService/electFenceSet/electFenceSet'
      })
   },
    getTopQuerySelect(queryS){
        let _this = this;
        const query = wx.createSelectorQuery()
        query.select(queryS).boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(res=>{
            // console.log(res);
             // res[0].height;
            _this.setData({
                topH:res
            })
        })
    },
    getBottomQuerySelect(queryS){
        let _this = this;
        const query = wx.createSelectorQuery()
        query.select(queryS).boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(res=>{
            // console.log(res);
            // res[0].height;
            _this.setData({
                bottomH:res
            })
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getTopQuerySelect('#head-box');
      this.getBottomQuerySelect('#bottom-bar');
      util.setNavBarTitle('电子围栏');
      let _this = this;
      this.getNowposition();
      // 实例化API核心类
      qqmapsdk = new QQMapWX({
          key: 'T2KBZ-DENW4-DKHU5-XPLOB-RBXH5-EBBES'
      });
      wx.getSystemInfo({
          success(res){
              setTimeout(function(){
                  let topH = _this.data.topH[0].height;
                  let bottomH = _this.data.bottomH[0].height;
                  _this.setData({
                      mapH:res.windowHeight - topH -bottomH
                  })
              },500)

          }
      })

  },
  //  获取当前的位置
    getNowposition(){
      let that = this;
        wx.getLocation( {
            success: function( res ) {
                console.log( res )
                that.setData( {
                    hasLocation: true,
                    location: {
                        longitude: res.longitude,
                        latitude: res.latitude
                    }
                })
            }
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let _this = this;
      // 调用接口
      qqmapsdk.search({
              keyword: '中恩',
              success: function (res) {
                  console.log(res);
                  _this.setData({
                      lon:res.data[0].location.lng  ,
                      lat:res.data[0].location.lat,
                      markers:[
                          {
                              iconPath: '/resources/pensionService/posi-red.png',
                              id: 0,
                              latitude: res.data[0].location.lat,
                              longitude:res.data[0].location.lng,
                              width: '74rpx',
                              height: '87rpx'
                          },
                          {
                              iconPath: '/resources/pensionService/now-position.png',
                              id: 1,
                              latitude: 39.1292945658,
                              longitude:117.4385476112,
                              width: '32rpx',
                              height: '32rpx'
                          }
                      ],
                      circles:[
                          {
                              latitude:res.data[0].location.lat,
                              longitude:res.data[0].location.lng,
                              radius:600,
                              color:'#f97975',
                              strokeWidth:.5,
                              fillColor:'#ffffff80'
                          }
                      ]
                  })
              },
              fail: function (res) {
                  console.log(res);
              },
              complete: function (res) {
                  console.log(res);
              }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
